<?php ob_start();?>
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include($root.'/standart/head.php');
include($root.'/standart/dateHeader.php');
include($root.'/standart/checkUser.php');
function goToProduct($success){
	echo '<script LANGUAGE="JavaScript">';
	echo 'window.location="/event?success='.$success.'";';
	echo '</script>';
}
$db = connect();

$eventTitle = $_POST["event_title"];
$id = -1;
if(isset($_POST["event_id"])){
	$id = $_POST["event_id"];
}else{
	$id = $_GET["event_id"];
}
$event_def = $_POST["event_def"];
$event_interval = $_POST["event_interval"];
$eventDate = $_POST["event_date"];
list($gun,$ay,$yil) = explode('/',$eventDate);
$yeni_tarih = $yil.'-'.$ay.'-'.$gun;

$successFlag = 'E';
if($id <> null || $id <> ''){
	if(isset($_GET["DELETE"])){
		$stmt ="DELETE FROM EVENTS
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('i', $id);
		if($statement->execute ()) {
			echo '<span style="color:green; font-weight:bolder;">Kayıt başarıyla silindi</span>';
		} else {
			echo '<span style="color:red; font-weight:bolder;">İşlem sırasında hata alındı </span>';
		}
		$statement->close();
	}else{
		$stmt ="UPDATE EVENTS 
					SET EVENT_TITLE = ?, 
						EVENT_DEFINITION = ?,
						START_TIME = ?,
						END_TIME = ?,
						EVENT_DATE = ?
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('sssssi', $eventTitle, $event_def, split("/", $event_interval)[0], split("/", $event_interval)[1], $yeni_tarih, $id);
		if($statement->execute ()) {
			$successFlag = 'E';
		}else {
			$successFlag = 'H';
		}
		$statement->close();
	}
}else{
	if(trim($eventTitle,' ') <> '' || trim($eventDate,' ') <> '' ){
		$stmt ="INSERT INTO EVENTS (EVENT_TITLE, EVENT_DEFINITION, EVENT_DATE, START_TIME, END_TIME) 
					VALUES(?,?,?,?,?)";
		$statement = $db->prepare($stmt);
		$statement->bind_param('sssss', $eventTitle, $event_def, $yeni_tarih, split("/", $event_interval)[0], split("/", $event_interval)[1]);
		if($statement->execute ()) {
			$id = $db->insert_id;
			$successFlag = 'E';
		}else {
			$successFlag = 'H';
		}
	}else{
		$successFlag = 'H';
	}
}
if(!isset($_GET["DELETE"]) && $_FILES["event_picture"]["name"] <> '' && $id >= 0){
	$allowedExts = array("gif", "jpeg", "jpg", "png");
	$temp = explode(".", $_FILES["event_picture"]["name"]);
	$extension = end($temp);
	if ((($_FILES["event_picture"]["type"] == "image/gif")
			|| ($_FILES["event_picture"]["type"] == "image/jpeg")
			|| ($_FILES["event_picture"]["type"] == "image/jpg")
			|| ($_FILES["event_picture"]["type"] == "image/pjpeg")
			|| ($_FILES["event_picture"]["type"] == "image/x-png")
			|| ($_FILES["event_picture"]["type"] == "image/png"))
			&& in_array($extension, $allowedExts)) {
				if ($_FILES["event_picture"]["error"] > 0) {
					echo "Return Code: ".$_FILES["event_picture"]["error"] . "<br>";
				} else {
					echo file_exists($root."/upload/event".$id.'.'.$extension);
					if (file_exists($root."/upload/event".$id.'.'.$extension)) {
						unlink($root."/upload/event".$id.'.'.$extension);
					}
					echo $root."/upload/event".$id.'.'.$extension;
					move_uploaded_file($_FILES["event_picture"]["tmp_name"], $root."/upload/event".$id.'.'.$extension);
					$imagePath = 'http://admin.gevezecafe.com/upload/event'.$id.'.'.$extension;
					$stmt = "UPDATE EVENTS SET EVENT_IMAGE = ? WHERE ID = ?";
					$st = $db->prepare($stmt);
					$st->bind_param('si', $imagePath, $id);
					if($st->execute()) {
						goToProduct('E');
					}else {
						goToProduct('H');
					}
					$st->close();
				}
			} else {
				echo "Invalid file";
				goToProduct('H');
			}
}else{
	goToProduct($successFlag);
}
disconnect($db);
?>
<?php ob_end_flush(); ?>